package system

import (
	"context"

	"go_admin/api/system/v1"
	"go_admin/internal/database/system/model/dto"
	"go_admin/internal/service"
)

func (c *ControllerV1) AuthRuleList(ctx context.Context, req *v1.AuthRuleListReq) (res *v1.AuthRuleListRes, err error) {
	var list []*dto.SysAuthRuleInfoRes
	res = &v1.AuthRuleListRes{
		Rules: make([]*dto.SysAuthRuleTreeRes, 0),
	}
	list, err = service.SysAuthRule().GetMenuListSearch(ctx, req)
	if req.Title != "" || req.Component != "" {
		for _, menu := range list {
			res.Rules = append(res.Rules, &dto.SysAuthRuleTreeRes{
				SysAuthRuleInfoRes: menu,
			})
		}
	} else {
		res.Rules = service.SysAuthRule().GetMenuListTree(0, list)
	}
	return
}
func (c *ControllerV1) AuthRuleAdd(ctx context.Context, req *v1.AuthRuleAddReq) (res *v1.AuthRuleAddRes, err error) {
	err = service.SysAuthRule().Add(ctx, req)
	return
}
func (c *ControllerV1) AuthRuleGetParams(ctx context.Context, req *v1.AuthRuleGetParamsReq) (res *v1.AuthRuleGetParamsRes, err error) {
	// 获取角色列表
	res = new(v1.AuthRuleGetParamsRes)
	res.Roles, err = service.SysRole().GetRoleList(ctx)
	if err != nil {
		return
	}
	res.Menus, err = service.SysAuthRule().GetIsMenuList(ctx)
	return
}
func (c *ControllerV1) AuthRuleGet(ctx context.Context, req *v1.AuthRuleGetReq) (res *v1.AuthRuleGetRes, err error) {
	res = new(v1.AuthRuleGetRes)
	res.Rule, err = service.SysAuthRule().Get(ctx, req.Id)
	if err != nil {
		return
	}
	res.RoleIds, err = service.SysAuthRule().GetMenuRoles(ctx, req.Id)
	return
}
func (c *ControllerV1) AuthRuleUpdate(ctx context.Context, req *v1.AuthRuleUpdateReq) (res *v1.AuthRuleUpdateRes, err error) {
	err = service.SysAuthRule().Update(ctx, req)
	return
}
func (c *ControllerV1) AuthRuleDelete(ctx context.Context, req *v1.AuthRuleDeleteReq) (res *v1.AuthRuleDeleteRes, err error) {
	err = service.SysAuthRule().DeleteMenuByIds(ctx, req.Ids)
	return
}
